

import urllib2, urllib
from indra.base import llsd

LOGINSERVER = "http://localhost:5100/login/do"


def send(url,data):
    
    payload = urllib.urlencode(data)
    #payload = llsd.format_xml(data)
    
    #headers = {"Content-type": "application/xml"}
    headers = {"Content-type": "application/x-www-form-urlencoded"}
    r = urllib2.Request(url,payload,headers)
    res = urllib2.urlopen(r)
    res_data_xml = res.read()
    res_data = llsd.parse(res_data_xml)
    
    return res_data
    
    
    

def login(username, password):
    """login to agent domain"""
    
    data={
        'username' : username,
        'credentials' : password
    }
    
    login_data = send(LOGINSERVER,data)
    print login_data
    seed_capability = login_data['seed-capability']
    print seed_capability
    
    # now request more capabilities
    caps_request={'caps-wanted' : ['inventory','god','shutdown']}
    caps_response = send(seed_capability,caps_request)
    
    return caps_response
    
    
if __name__=="__main__":
    print login("hans","secret")
    
    
    
    


